<?php


namespace app\admin\controller\member2;


use app\common\controller\Adminbase;
use think\Db;
use think\facade\Session;

class Order extends Adminbase
{
    protected $searchFields = 'order_no';

    protected function initialize()
    {
        $this->where = array();
        if (Session::get("admin.roleid") == 3) {
            $admin_where[] = ['area_id', '=', Session::get("admin.area_id")];
            $admin_where[] = ['roleid', '=', 4];
            $ying_ids=Db::name('admin')->where($admin_where)->column('id');
            $business_ids = Db::name('business')
                ->where('belong_admin_id', 'in',$ying_ids)
                ->column('id');
            $this->where[] = ['o.business_id', 'in', $business_ids];
        }
        if (Session::get("admin.roleid") == 4) {
            $business_ids = Db::name('business')->where('belong_admin_id', Session::get("admin.id"))->column('id');
            $this->where[] = ['o.business_id', 'in', $business_ids];
        }
        parent::initialize();
    }

    public function index()
    {
        if ($this->request->isAjax()) {
            $page = input('page/d', 1);
            $limit = input('limit/d', 10);
            $where = array();
            //可能不存在
            $filter = input('filter');
            if ($filter) {
                $filter = json_decode($filter, true);
                if (isset($filter['order_no'])) {
                    $where[] = ['o.order_no', 'like', '%' . $filter['order_no'] . '%'];
                }
                if (isset($filter['id'])) {
                    $where[] = ['o.id', '=', $filter['id']];
                }
                if (isset($filter['pay_status'])) {
                    $where[] = ['o.pay_status', '=', $filter['pay_status']];
                }
                if (isset($filter['pay_type'])) {
                    $where[] = ['o.pay_type', '=', $filter['pay_type']];
                }
                if (isset($filter['status'])) {
                    $where[] = ['o.status', '=', $filter['status']];
                }
                if (isset($filter['business_id'])) {
                    $where[] = ['o.business_id', '=', $filter['business_id']];
                }
                if (isset($filter['member_id'])) {
                    $where[] = ['o.member_id', '=', $filter['member_id']];
                }
                if (isset($filter['create_time'])) {
                    $create_time = explode(' - ', $filter['create_time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where[] = ['o.create_time', 'between', [$create_time[0], $create_time[1]]];
                }
                if (isset($filter['pay_time'])) {
                    $pay_time = explode(' - ', $filter['pay_time']);
                    foreach ($pay_time as $k => $v) {
                        $pay_time[$k] = strtotime($v);
                    }
                    $where[] = ['o.pay_time', 'between', [$pay_time[0], $pay_time[1]]];
                }
            }
            $search = input('search');
            if ($search) {
                $where[] = ['o.order_no', 'like', '%' . $search . '%'];
            }
            $_list = Db::name('order')
                ->alias('o')
                ->join('business b', 'o.business_id=b.id')
                ->join('member me', 'o.member_id=me.id')
                ->field('o.*,b.business_name,me.nickname')
                ->where($where)
                ->where($this->where)
                ->page($page, $limit)
                ->order('create_time desc')
                ->select();
            foreach ($_list as $k => $v) {
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
                $_list[$k]['pay_time'] = date('Y-m-d H:i:s', $v['pay_time']);
            }
            $total = Db::name('order')
                ->alias('o')
                ->join('business b', 'o.business_id=b.id')
                ->join('member me', 'o.member_id=me.id')
                ->where($where)
                ->where($this->where)
                ->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        return $this->fetch('member2/order/index');
    }

    public function detail()
    {
        $order_id = $this->request->param('id/d', 0);
        $order_info = Db::name('order')->find($order_id);
        $order_info['dispatch_time'] = date('Y-m-d H:i:s', $order_info['dispatch_time']);
        $order_info['deliver_time'] = date('Y-m-d H:i:s', $order_info['deliver_time']);
        //订单位置
        $address_info = Db::name('delivery_address')->find($order_info['address_id']);
        //配送信息

        $delivery = Db::name('delivery')->where('id', $order_info['delivery_id'])->find();
        //商品信息
        $order_detail = Db::name('order_detail')
            ->alias('od')
            ->join('goods', 'od.goods_id=goods.id')
            ->where('order_id', $order_id)
            ->field('od.*,goods.img,goods.specs')
            ->find();
        $this->assign("order_info", $order_info);
        $this->assign("address_info", $address_info);
        $this->assign("delivery", $delivery);
        $this->assign("order_detail", $order_detail);
        return $this->fetch('member2/order/detail');
    }

}